#include <cstddef>
#include <iostream>
#include <utility>
#define MIN_SIZE 6
template <typename ValueT> class vector {
public:
  using value_type = ValueT;
  using reference = ValueT &;
  using const_reference = const ValueT &;
  using iterator = ValueT *;
  using const_iterator = ValueT const *;
  using size_type = std::size_t;
  using difference_type = std::ptrdiff_t;

public:
  vector() : _data(nullptr), _size(0), _capacity(0) {}
  // 复制构造
  vector(const vector<value_type> &other) {
    _data = static_cast<value_type *>(new value_type[other.capacity()]);
    for (int i = 0; i < other.size(); ++i) {
      new (&_data[i]) ValueT(other[i]);
    }
    _size = other._size;
  }
  // 移动构造
  vector(vector<value_type> &&other) {
    if (other.size() > 0) {
      _data = other._data;
      _size = other._size;
      _capacity = other._capacity;
      other._data = nullptr;
      other._size = 0;
      other._capacity = 0;
    }
  }
  // 赋值运算符
  vector<value_type> &operator=(const vector<value_type> &other) {
    if (other._size > 0) {
      _data = static_cast<value_type *>(new value_type[other._capacity]);
      _capacity = other._capacity;
      _size = other._size;
      for (int i = 0; i < other._size; i++) {
        _data[i] = other[i];
      }
    }
    return *this;
  }
  // 迭代器
  iterator begin() { return _data; }

  const_iterator begin() const { return _data; }

  iterator end() { return _data + _size; }

  const_iterator end() const { return _data + _size; }

  value_type *data() { return _data; }

  value_type const *data() const { return _data; }

  size_type size() const { return _size; }

  size_type capacity() const { return _capacity; }
  // 判断容器是否为空
  bool empty() { return _size == 0; }
  // 删除所有元素
  void clear() {
    for (int i = 0; i < _size; i++) {
      _data[i].~ValueT();
    }
    _size = 0;
  }
  //  增加元素
  void push_back(const value_type &value) { emplace_back(value); }

  void push_back(value_type &&value) const { emplace_back(std::move(value)); }

  template <typename... Args> reference emplace_back(Args &&...args) {
    // 有剩余空间
    if (_size < _capacity) {
      // 直接插入元素
      new (&_data[_size++]) ValueT(std::forward<Args>(args)...);
      return _data[_size - 1];
    }
    // 第一次插入元素
    if (_capacity == 0) {
      _data = static_cast<value_type *>(new value_type[MIN_SIZE]);
      _capacity = MIN_SIZE;
      new (&_data[_size++]) ValueT(std::forward<Args>(args)...);
      return _data[_size - 1];
    }
    // 容器已满, 对容器进行扩容
    if (_size == _capacity) {
      int new_capacity = _capacity * 2;
      value_type *temp = new value_type[new_capacity];
      for (int i = 0; i < _size; i++) {
        // 拷贝元素
        new (&temp[i]) value_type(std::move(_data[i]));
      }
      // 插入元素
      new (&temp[_size++]) ValueT(std::forward<Args>(args)...);
      // 删除旧数据
      for (int i = 0; i < _size; i++) {
        // 删除元素
        _data[i].~value_type();
      }
      // 释放空间
      delete[] _data;
      _data = temp;
      _capacity = new_capacity;
      return _data[_size - 1];
    }
    return _data[_size - 1];
  }

  // 删除最后一个元素
  void pop_back() {
    // 判断是否有元素
    if (!empty()) {
      _data[--_size].~ValueT();
    }
  }
  // 访问元素
  reference operator[](int i) { return _data[i]; }
  reference at(int i) {
    if (i >= _size) {
      throw std::out_of_range("下标越界");
    } else {
      return _data[i];
    }
  }

  ~vector() {
    if (_data != nullptr) {
      for (int i = 0; i < _size; i++) {
        _data[i].~ValueT();
      }
      // 释放内存
      delete[] _data;
    }
  }

private:
  value_type *_data;
  size_type _size;
  size_type _capacity;
};

int main() {
  vector<int> v;
  for (int i = 0; i < 50; i++) {
    v.push_back(i);
  }
  std::cout << v.size() << std::endl;
  std::cout << v.capacity() << std::endl;
  for (int i = 0; i < 50; i++) {
    std::cout << v[i] << " ";
  }
  std::cout << std::endl;
  return 0;
}